package utildate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Localclass {


//      日期时间日历操作：
//          --- Date + SimpleDateFormat + Calendar
//          --- LocalDate + LocalTime + LocalDateTime + DateTimeFormatter
//              LocalDate 类 是JDK1.8提供的专门用于操作年月日的类
//              LocalTime 类 是JDK1.8提供的专门用于操作时分秒的类
//              LocalDateTime 类 是JDK1.8提供的专门用于操作年月日时分秒的类
//
//              DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒") : 指定日期时间格式的模板对象
//
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now); // now = 2025-10-22T16:24:59.327553
        // 获取年、月、日、时、分、秒的值
        int year = now.getYear();//获取当前年
        System.out.println("year = " + year);
        int month = now.getMonthValue();//获取当前月
        System.out.println("month = " + month);
        int day = now.getDayOfMonth();//获取当前日
        System.out.println("day = " + day);
        int hour = now.getHour();//获取当前时
        System.out.println("hour = " + hour);
        int minute = now.getMinute();//获取当前分
        System.out.println("minute = " + minute);
        int second = now.getSecond();//获取当前秒
        System.out.println("second = " + second);
        int nanoSecond = now.getNano();//获取当前纳秒值
        System.out.println("nanoSecond = " + nanoSecond);

        // 修改年、月、日、时、分、秒的值
        LocalDateTime localDateTime = now.withYear(2000).withMonth(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        System.out.println("localDateTime = " + localDateTime); // localDateTime = 2000-01-01T00:00

        // 将当前的now对象转成 yyyy年MM月dd日HH时mm分ss秒字符串格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒");
        String format = now.format(dateTimeFormatter);
        System.out.println("format = " + format); // format = 2025年10月22日16时28分41秒


        // 将字符串转为： LocalDateTime对象
        String s = "2025-10-22 16:28:41";
        LocalDateTime parse = LocalDateTime.parse(s , DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("parse = " + parse);
    }

}
